package cn.ibobo.lease.auth.controller;

import cn.ibobo.lease.auth.service.impl.AdminServiceImpl;
import cn.ibobo.lease.auth.service.impl.UserServiceImpl;
import cn.ibobo.lease.common.dto.AdminDTO;
import cn.ibobo.lease.common.dto.UserDTO;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.Md5Util;
import cn.ibobo.lease.common.util.ResultVOUtil;
import cn.ibobo.lease.common.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description 管理员登陆控制器
 * @Author lililiang
 * @Date 2018-12-09 18:17:26
 */

@RestController
@Api(tags = "管理员登陆控制器")
@RequestMapping("login")
public class AdminLoginController {

    private static final Log LOGGER = LogFactory.getLog(AdminLoginController.class);

    @Autowired
    private AdminServiceImpl service;
    @Autowired
    private UserServiceImpl userServiceImpl;
    @Autowired
    private ResultVOUtil<Object> resultObject;





    /**
     * @Description 管理端登陆
     * @Author lililiang
     * @Date 2018/12/13 19:02
     * @Param [id]
     * @Return cn.ibobo.lease.common.vo.ResultVO<java.lang.Object>
     */
    @ApiOperation(value = "管理端登陆", response = AdminDTO.class)
    @PostMapping("login")
    public ResultVO<Object> login(
            @ApiParam(value = "用户名称",required = true) @RequestParam(value = "name") String name,
            @ApiParam(value = "密码",required = true) @RequestParam(value = "passwd") String passwd,
            HttpServletRequest req
    ){
        try {
            AdminDTO entityDTO = new AdminDTO();
            entityDTO.setName(name);
            entityDTO.setPasswd(Md5Util.md5Suffix(passwd));
            AdminDTO adminDTO = service.login(entityDTO, req);
            if(adminDTO == null){
                return resultObject.success(ResultEnum.ERR_NAME_PSWD.getCode(), ResultEnum.ERR_NAME_PSWD.getMsg());
            }else{
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), adminDTO);
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * @Description APP登陆
     * @Author lililiang
     * @Date 2018/12/13 19:02
     * @Param [id]
     * @Return cn.ibobo.lease.common.vo.ResultVO<java.lang.Object>
     */
    @ApiOperation(value = "APP登陆", response = AdminDTO.class)
    @PostMapping("appLogin")
    public ResultVO<Object> appLogin(
            @ApiParam(value = "用户名称",required = true) @RequestParam(value = "name") String name,
            @ApiParam(value = "密码",required = true) @RequestParam(value = "passwd") String passwd,
            HttpServletRequest req
    ){
        try {
            UserDTO entityDTO = new UserDTO();
            entityDTO.setName(name);
            entityDTO.setPasswd(Md5Util.md5Suffix(passwd));
//            entityDTO.setName("13284421055");
//            entityDTO.setPasswd(Md5Util.md5Suffix("12"));
            UserDTO userDTO = userServiceImpl.appLogin(entityDTO, req);
            if(userDTO == null){
                return resultObject.success(ResultEnum.ERR_NAME_PSWD.getCode(), ResultEnum.ERR_NAME_PSWD.getMsg());
            }else{
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), userDTO);
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }



}